package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.dto.ProjectDto;
import com.jyy.prefabricated.dto.PurchaseItemsDto;
import com.jyy.prefabricated.entity.ProjectInfo;
import com.jyy.prefabricated.service.ProjectInfoService;
import com.jyy.prefabricated.utils.OrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 项目
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ProjectInfoController {

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 新增项目
     * @param dto
     * @return
     */
    @PostMapping("/projects")
    public AjaxResult insert(@Valid @RequestBody ProjectDto dto){
        log.info("Rest request to insert projectInfo:{}",dto);
        int id = projectInfoService.insert(dto);
        ProjectInfo result = projectInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新项目
     * @param dto
     * @return
     */
    @PutMapping("/projects")
    public AjaxResult update(@Valid @RequestBody ProjectDto dto){
        log.info("Rest request to update projectInfo:{}",dto);
        projectInfoService.update(dto);
        ProjectInfo result = projectInfoService.selectById(dto.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    @PutMapping("/projects-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update projects:{}",dto);
        projectInfoService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    @DeleteMapping("/projects-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete projects by id:{}",dto);
        int row = projectInfoService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/projects/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find projectInfo by id:{}",id);
        ProjectInfo result = projectInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除项目
     * @param id
     * @return
     */
    @DeleteMapping("/projects/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete projectInfo by id:{}",id);
        int row = projectInfoService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询项目
     * @param categoryId 分类id
     * @param type 类型
     * @param status 状态
     * @param name 项目名称
     * @param ascColumn 升序排序字段
     * @param descColumn 降序排序字段
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/projects")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId,@RequestParam(required = false) Integer type,@RequestParam(required = false) Integer status, @RequestParam(required = false, defaultValue = "") String name,@RequestParam(required = false,defaultValue = "") String ascColumn,@RequestParam(required = false,defaultValue = "") String descColumn, Pageable pageable){
        String orderStr = OrderUtil.genOrderStr(ascColumn, descColumn);
        log.info("Rest request to find projectInfo by params categoryId:{},type:{},name:{},status:{},orderStr:{}", categoryId, type, name,status,orderStr);
        List<Integer> categoryIds = projectInfoService.selectDeepCategoryIds(categoryId);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ProjectInfo> projects = projectInfoService.selectDeepByParams(categoryIds, type, name, status, orderStr, null);
        PageInfo<ProjectInfo> result = new PageInfo<>(projects);
        return AjaxResult.success(result);
    }
    /**
     * 分页查询项目
     * @param categoryId 分类id
     * @param status 状态
     * @param name 项目名称
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/purchase-items")
    public AjaxResult selectPurchaseItems(@RequestParam(required = false) Integer categoryId, @RequestParam(required = false) Integer status, @RequestParam(required = false, defaultValue = "") String name, Pageable pageable){
        log.info("Rest request to find projectInfo by params categoryId:{},name:{},status:{}", categoryId, name,status);
        List<Integer> categoryIds = projectInfoService.selectDeepCategoryIds(categoryId);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<PurchaseItemsDto> purchaseItemsDtos = projectInfoService.selectPurchaseItems(categoryIds, name, status);
        PageInfo<PurchaseItemsDto> result = new PageInfo<>(purchaseItemsDtos);
        return AjaxResult.success(result);
    }
    /**
     * 查询项目
     * @param categoryId 分类id
     * @param type 类型
     * @param status 状态
     * @param name 项目名称
     * @param ascColumn 升序排序字段
     * @param descColumn 降序排序字段
     * @return
     */
    @GetMapping("/projects-list")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer categoryId,@RequestParam(required = false) Integer type,@RequestParam(required = false) Integer status,@RequestParam(required = false, defaultValue = "") String name,@RequestParam(required = false,defaultValue = "") String ascColumn,@RequestParam(required = false,defaultValue = "") String descColumn,@RequestParam(required = false) Integer deadlineType){
        String orderStr = OrderUtil.genOrderStr(ascColumn, descColumn);
        log.info("Rest request to find projectInfo by params categoryId:{},type:{},name:{},status:{},orderStr:{}", categoryId, type, name,status,orderStr);
        List<Integer> categoryIds = projectInfoService.selectDeepCategoryIds(categoryId);
        List<ProjectInfo> result = projectInfoService.selectDeepByParams(categoryIds, type, name, status,orderStr, deadlineType);
        return AjaxResult.success(result);
    }
}
